﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.Utils;
using System.Threading;
using HostAssistantCore;
using HostAssistantBLL;
namespace HostAssistant
{
    public partial class setting : U1UserControl
    {
        public setting()
        {
            InitializeComponent();
          txts.ReadOnly=  txtpath.ReadOnly= txtname.ReadOnly = true;
        }
        #region sitelist_Load

        private void sitelist_Load(object sender, EventArgs e)
        {
            var set = Data<HostAssistantCore.setting>.GetInstance().GetModelList("select *  from setting", null);
            var mysql = set.FirstOrDefault(o => o.type == "mysql");
            txtname.Text = mysql?.ip ?? "";
            txtport.Text = mysql?.port ?? "";
            txtuser.Text = mysql?.username ?? "";
            txtpwd.Text = mysql?.userpwd ?? "";
            txtpath.Text = mysql?.Path ?? "";
            textEdit1.Text = mysql?.MyPath ?? "";
            var mssql = set.FirstOrDefault(o => o.type == "mssql");
            txtmm.Text = mssql?.userpwd ?? "";
            txtzh.Text = mssql?.username ?? "";
            txts.Text = mssql?.Path ?? "";



            var PathSet = Data<HostAssistantCore.PathSet>.GetInstance().GetModelList("select *  from PathSet", null);
            var PathSetmodel = PathSet.FirstOrDefault();
            txtsite.Text = PathSetmodel?.SitePath ?? "";
            txtbf.Text = PathSetmodel?.SiteBakPath ?? "";
            txtdbb.Text = PathSetmodel?.DbBakPath ?? "";



        }

        #endregion      

        #region mysql 配置 
        /// <summary>
        /// mysql 配置 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click_1(object sender, EventArgs e)
        {

            try
            {

          
            if(String.IsNullOrWhiteSpace(txtname.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("服务器名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (String.IsNullOrWhiteSpace(txtport.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("端口号不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (String.IsNullOrWhiteSpace(txtuser.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("登录名不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (String.IsNullOrWhiteSpace(txtpwd.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("密码不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }


            if (String.IsNullOrWhiteSpace(txtpath.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("安装目录不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }


                if (String.IsNullOrWhiteSpace(textEdit1.Text))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("数据库存储目录不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }


                MySqlDbHelper mdb = new MySqlDbHelper();
            mdb.Initialize(txtname.Text.Trim(), "mysql", txtuser.Text.Trim(), txtpwd.Text.Trim(), txtport.Text.Trim());
            string r= mdb.ConnectionTest();
            if (r==null) 
            {
                Data<HostAssistantCore.setting>.GetInstance().Delete("setting", "type='mysql'",null);
                HostAssistantCore.setting setting = new HostAssistantCore.setting();
                setting.type = "mysql";
                setting.ip = txtname.Text.Trim();
                setting.port = txtport.Text.Trim();
                setting.username = txtuser.Text.Trim();
                setting.userpwd = txtpwd.Text.Trim();
                setting.Path = txtpath.Text.Trim();
                setting.MyPath = textEdit1.Text.Trim();
                 


                string query = " INSERT INTO setting(MyPath, Path, type,ip,port,username,userpwd)VALUES(@MyPath, @Path, @type,@ip,@port,@username,@userpwd)   ";
                int id = Data<HostAssistantCore.setting>.GetInstance().Add(query, setting);
                if(id>0)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("mysql配置成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("mysql配置保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }







            }
            else
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(r, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }



            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString());
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }


        }
        #endregion

        #region mssql
        /// <summary>
        /// mssql
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton2_Click(object sender, EventArgs e)
        {

            try
            {
            if (String.IsNullOrWhiteSpace(txtzh.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("登录名不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (String.IsNullOrWhiteSpace(txtmm.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("密码不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }


                if (String.IsNullOrWhiteSpace(txts.Text))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("数据库存储目录不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }



                string sqlname = this.txtzh.Text.Trim().ToString();
            string sqlpass = this.txtmm.Text.Trim().ToString();
                SqlDbHelper.Initialize("master", sqlname, sqlpass);
            if (SqlDbHelper.ConnectionTest())
            {    
                Data<HostAssistantCore.setting>.GetInstance().Delete("setting", "type='mssql'", null);
                 

                HostAssistantCore.setting setting = new HostAssistantCore.setting();
                setting.type = "mssql";
             
                setting.username = sqlname;
                setting.userpwd = sqlpass;

                    setting.Path = txts.Text.Trim();




                    string query = " INSERT INTO setting(Path,  type,username,userpwd)VALUES( @Path, @type,@username,@userpwd)   ";
                int id = Data<HostAssistantCore.setting>.GetInstance().Add(query, setting);
                if (id > 0)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("mssql配置成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("mssql配置保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }

            }
            else
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("mssql配置保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString());
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }


        #endregion

        #region 选择路径


        private void simpleButton7_Click(object sender, EventArgs e)
        {

            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string foldPath = dialog.SelectedPath;
                txts.Text = foldPath;


            }

        }

        private void simpleButton6_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string foldPath = dialog.SelectedPath;
                txtpath.Text = foldPath;


            }
        }


        private void simpleButton5_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string foldPath = dialog.SelectedPath;
                txtsite.Text = foldPath;


            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string foldPath = dialog.SelectedPath;
                txtbf.Text = foldPath;


            }
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string foldPath = dialog.SelectedPath;
                txtdbb.Text = foldPath;


            }
        }
        #endregion

        #region 保存默认路径

        private void simpleButton1_Click_2(object sender, EventArgs e)
        {

            try
            { 
            if (String.IsNullOrWhiteSpace(txtsite.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("站点路径不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }


            if (String.IsNullOrWhiteSpace(txtbf.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("站点备份路径不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }



            if (String.IsNullOrWhiteSpace(txtdbb.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("数据库备份路径不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }






            Data<HostAssistantCore.PathSet>.GetInstance().Delete("PathSet", "1=1", null);


            HostAssistantCore.PathSet setting = new HostAssistantCore.PathSet();

            setting.SitePath = txtsite.Text.Trim();
            setting.SiteBakPath = txtbf.Text.Trim();
            setting.DbBakPath = txtdbb.Text.Trim();



            string query = " INSERT INTO PathSet(SitePath,SiteBakPath,DbBakPath)VALUES(@SitePath,@SiteBakPath,@DbBakPath)   ";
            int id = Data<HostAssistantCore.PathSet>.GetInstance().Add(query, setting);
            if (id > 0)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("默认路径配置成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            else
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("默认路径配置保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }



            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString());
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }


        }



        #endregion

        /// <summary>
        /// 数字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtport_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
            if (e.KeyChar >= '0' && e.KeyChar <= '9')
            {
                e.Handled = false;
            }
        }

        private void simpleButton8_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string foldPath = dialog.SelectedPath;
                textEdit1.Text = foldPath;


            }
        }
    }
}